import 'package:base_plugin/base_plugin.dart';
import 'package:dio/dio.dart';

class HttpProductService {
  /// 产品列表
  static Future<Response> productList([int noLink]) {
    return BaseConfig.httpBase.get('/api/products', {
      'no_link': noLink,
    });
  }

  /// 材料清单列表
  static Future<Response> productBomList() {
    return BaseConfig.httpBase.get('/api/boms', null);
  }

  /// 批量禁用产品清单
  static Future<Response> productBatchDeactivate(List<int> ids) {
    return BaseConfig.httpBase.postJson('/api/products/batch_deactivate', {
      'product_ids': ids,
    });
  }

  static Future<Response> productInventoryBatchAdjust(
    List products,
    String notes,
    String date,
    List attachment_ids,
  ) {
    return BaseConfig.httpBase.postJson('/api/inventories/batch_adjust', {
      'products': products,
      'notes': notes,
      'issued_at': date,
      'attachment_ids': attachment_ids ?? [],
    });
  }

  /// 批量激活产品清单
  static Future<Response> productBatchActivate(List<int> ids) {
    return BaseConfig.httpBase.postJson('/api/products/batch_activate', {
      'product_ids': ids,
    });
  }

  /// 批量删除产品清单
  static Future<Response> productBatchDelete(List<int> ids) {
    return BaseConfig.httpBase.postJson('/api/products/batch_delete', {
      'product_ids': ids,
    });
  }

  static Future<Response> productCreate(
    String categoryId,
    String name,
    String code,
    String barcode,
    String unitType,
    String description,
    int enableSell,
    String currencyCode,
    double price,
    int taxId,
    int isTaxInclusive,
    int enablePurchase,
    String purchaseCurrencyCode,
    double purchasePrice,
    int purchaseTaxId,
    int purchaseIsTaxInclusive,
    int enableInventory,
    int inventoryLeft,
    List boms,
    List attachmentIds,
    List categories,
  ) {
    return BaseConfig.httpBase.postJson('/api/products', {
      "category_id": categoryId,
      "name": name,
      "code": code,
      "barcode": barcode,
      "unit_type": unitType,
      "description": description,
      "enable_sell": enableSell,
      "currency_code": currencyCode,
      "price": price,
      "tax_id": taxId,
      "is_tax_inclusive": isTaxInclusive,
      "enable_purchase": enablePurchase,
      "purchase_currency_code": purchaseCurrencyCode,
      "purchase_price": purchasePrice,
      "purchase_tax_id": purchaseTaxId,
      "purchase_is_tax_inclusive": purchaseIsTaxInclusive,
      "enable_inventory": enableInventory,
      "inventory_left": inventoryLeft ?? 0,
      'boms': boms ?? [],
      'attachment_ids': attachmentIds ?? [],
      'categories': categories ?? [],
    });
  }

  static Future<Response> productUpdate(
    int productId,
    String categoryId,
    String name,
    String code,
    String barcode,
    String unitType,
    String description,
    int enableSell,
    String currencyCode,
    double price,
    int taxId,
    int isTaxInclusive,
    int enablePurchase,
    String purchaseCurrencyCode,
    double purchasePrice,
    int purchaseTaxId,
    int purchaseIsTaxInclusive,
    int enableInventory,
    int inventoryLeft,
    List boms,
    List attachmentIds,
    List categories,
  ) {
    return BaseConfig.httpBase.putJson('/api/products/$productId', {
      "category_id": categoryId,
      "name": name,
      "code": code,
      "barcode": barcode,
      "unit_type": unitType,
      "description": description,
      "enable_sell": enableSell,
      "currency_code": currencyCode,
      "price": price,
      "tax_id": taxId,
      "is_tax_inclusive": isTaxInclusive,
      "enable_purchase": enablePurchase,
      "purchase_currency_code": purchaseCurrencyCode,
      "purchase_price": purchasePrice,
      "purchase_tax_id": purchaseTaxId,
      "purchase_is_tax_inclusive": purchaseIsTaxInclusive,
      "enable_inventory": enableInventory,
      "inventory_left": inventoryLeft,
      'boms': boms ?? [],
      'attachment_ids': attachmentIds ?? [],
      'categories': categories ?? [],
    });
  }

  static Future<Response> showProduct(String productId) {
    return BaseConfig.httpBase.get('/api/products/$productId', {});
  }

  static Future<Response> deleteProduct(int productId) {
    return BaseConfig.httpBase.deleteJson('/api/products/$productId', {});
  }

  static Future<Response> activateProduct(int productId) {
    return BaseConfig.httpBase
        .postJson('/api/products/$productId/activate', {});
  }

  static Future<Response> deactivateProduct(int productId) {
    return BaseConfig.httpBase
        .postJson('/api/products/$productId/deactivate', {});
  }
}
